package mx.itesm.gda.giliberto;

import java.util.Date;
import java.util.Formatter;

import android.R.color;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class GUIManager {

	// Custom views
    private ViewGroup overlayView;
    private ImageView startButton;
    private ImageView instructionsImg;
    private TextView timeTextView;
    private long startTime;
    
    // The main application context
    private Context applicationContext;
    
    // A Handler for working with the gui from other threads
    private Handler mainActivityHandler;
    
    private final String TIME_LEFT_FORMAT = "%s left";
    private final long TIME_TOTAL_MILIS = 60000; 
    
    // Flags for our Handler
    public static final int HIDE_INSTRUCTIONS = 0;    
    
    private Handler mHandler;
    
    /** Initialize the GUIManager. */
    public GUIManager(Context context)
    {
    	// Load our overlay view
    	// This view will add content on top of the camera / opengl view
    	overlayView = (ViewGroup) View.inflate(context, R.layout.interface_overlay, null);
    	
    	// Store the application context
    	applicationContext = context;
    	
    	// Create a Handler from the current thread
    	// This is the only thread that can make changes to the GUI,
    	// so we require a handler for other threads to make changes
        mainActivityHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case HIDE_INSTRUCTIONS:
                    	startButton.setOnClickListener(null);
                    	
                    	if (startButton != null) {
                    		startButton.setVisibility(View.INVISIBLE);
                    	}
                    	if (instructionsImg != null) {
                    		instructionsImg.setVisibility(View.INVISIBLE);
                    	}
                    	break;
                }
            }
        };
    }
    
    public void initButtons()
    {
    	if (overlayView == null)
    		return;
    	
        startButton = (ImageView) overlayView.findViewById(R.id.start_button_overlay);
        startButton.setClickable(true);
        startButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Message msg = new Message();
            	msg.what = HIDE_INSTRUCTIONS;
            	sendThreadSafeGUIMessage(msg);
            	
            	initTimeText();
            	startTime();
            }
        });
        
        instructionsImg = (ImageView)overlayView.findViewById(R.id.find_giliberto_overlay);
        instructionsImg.setBackgroundResource(R.drawable.find_giliberto);
        
    }
    
    /** Init the time textView **/
    protected void initTimeText() 
    {
    	timeTextView = (TextView)overlayView.findViewById(R.id.timeTextView);
    	timeTextView.setVisibility(View.VISIBLE);
    	
    	long seconds = TIME_TOTAL_MILIS / 1000;
    	long minutes = seconds / 60;
    	seconds = seconds % 60;
    	
    	setTimeViewText(minutes, seconds);
    	
    }
    
    /** Deinit the time textView **/
    protected void deinitTimeText() 
    {
		timeTextView = null;
	}
    
    /** Start Time on the Counter **/
    protected void startTime() 
    {
		mHandler = new Handler();
		
		if (startTime == 0L) 
		{
            startTime = System.currentTimeMillis();
            mHandler.removeCallbacks(mUpdateTimeTask);
            mHandler.postDelayed(mUpdateTimeTask, 100);
        }		
	}
    
    private void setTimeViewText(long minutes, long seconds)
    {
    	String str;
    	if (seconds < 10)
    		str = minutes+":0"+seconds+" left";
    	else
    		str = minutes+":"+seconds+" left";
    	timeTextView.setText(str);
    }
    
    /** Clear the button listeners. */
    public void deinitButtons()
    {
    	if (overlayView == null)
    		return;
    	
        startButton.setOnClickListener(null);
        
        startButton = null;
        
        instructionsImg = null;
    }
    
    
    /** Send a message to our gui thread handler. */
    public void sendThreadSafeGUIMessage(Message message)
    {
    	mainActivityHandler.sendMessage(message);
    }
    
    /** Getter for the overlay view. */
    public View getOverlayView()
    {
    	return overlayView;
    }
    
    private Runnable mUpdateTimeTask = new Runnable() {
		   public void run() {
		       final long start = startTime;
		       long millis = System.currentTimeMillis() - start;
		       millis = TIME_TOTAL_MILIS - millis;
		       int seconds = (int) (millis / 1000);
		       int minutes = seconds / 60;
		       seconds     = seconds % 60;

		       setTimeViewText(minutes, seconds);
		     
		       mHandler.postAtTime(this,
		               start + (((minutes * 60) + seconds + 1) * 1000));
		   }
		};
}